package com.ease.database.initializer;

import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import com.ease.common.util.Detect;
import com.ease.common.util.FileUtil;
import com.ease.common.vo.MapParameter;
import com.ease.dao.JdbcTemplateDataAccessObject;
import com.obas.test.ApplicationContextUserContextTest;

/**
 * @author nathanleewei
 *
 */
public class IndicatorTypeInitializer extends ApplicationContextUserContextTest {

	@Autowired
	JdbcTemplateDataAccessObject jdbcTemplateDataAccessObject;

	@Test
	public void test() {
		String string = FileUtil.readClassPathResourceToString(IndicatorTypeInitializer.class, "IndicatorType.txt");

		String[] rows = StringUtils.delimitedListToStringArray(string, "\r\n");
		MapParameter parameter = null;
		if (null != rows) {
			for (String row :rows) {
				String[] values = StringUtils.commaDelimitedListToStringArray(row);
				if (null == values || values.length < 2) {
					continue;
				}
				String name = Detect.trim(values[0]);
				String code = Detect.trim(values[1]);

				parameter = new MapParameter();
				parameter.put("code", code);
				//
				List<Map<String, Object>> timeDimensions = jdbcTemplateDataAccessObject.find("SELECT * FROM MM_INDICATORTYPE T WHERE T.CODE=:code", parameter);
				if (!notEmpty(timeDimensions)) {
					parameter.put("name", name);

					jdbcTemplateDataAccessObject
							.create("INSERT INTO MM_INDICATORTYPE(ID, NAME, CODE)VALUES(SEQ_MM_INDICATORTYPE.NEXTVAL, :name, :code)",
									parameter);
				}
			}
		}

	}

}
